<template>
	<view>
		<!-- //sfd ref="form1" :rules="rules" -->
		<u-form :model="userInfo" ref="uForm" class="u-m-l-50 u-m-r-50">
			<u-form-item label="姓名">
				<u-input v-model="userInfo.username" />
			</u-form-item>
			<u-form-item label="上传图片">
				<u-upload ref="uUpload" :auto-upload="false" max-count="1"></u-upload>

			</u-form-item>
		</u-form>
		<u-button type="primary" text="确定" @click="submit">提交按钮</u-button>
	</view>

</template>

<script>
	const db=uniCloud.database()
	export default {
		data() {
			return {
				name: '', // 昵称
				userInfo: {
					
					username: "",
					head_image:""
				},
				_id:"",
			}
		},
		onLoad(options) {
			this.name = options.name
			//获取缓存中的数据
			this.userInfo=uni.getStorageSync('storage_user')
		},
		onShow(){
			
		},
		methods: {
			// 登录提交
			async submit() {

				const temp_path=this.$refs.uUpload.lists[0].file.path
				const image_name=this.$refs.uUpload.lists[0].file.name
				let _this=this
				let _uni=uni
				uniCloud.uploadFile({
						filePath: temp_path, //文件的临时路径
						cloudPath:image_name, //在云存储中的名字
						success: function(res) {
							_this.userInfo.head_image= res.fileID;

							db.collection("user").where({
								_id:_this.userInfo._id
							}).update({
								username:_this.userInfo.username,
								head_image:_this.userInfo.head_image
							}).then(res=>{
								if(!res.result.code){
									_this.$u.toast('修改对象成功！')
									_uni.setStorageSync("storage_user",_this.userInfo)
									setTimeout(()=>{
										//跳转我的页
										 _uni.switchTab({
											url: '/pages/center/index'
										});
									},1500)
								}
							})
						}
					})	

				}
			}
		};
</script>

<style lang="scss" scoped>
	.u-border-bottom {
		margin-bottom: 40rpx !important;
	}
</style>
